<?php

namespace app\api\controller;

use app\BaseController;
use app\common\model\JieSuanDocModel;
use think\facade\Db;

class  JieSuan extends BaseController
{

    /**
     * @var  JieSuanDocModel
     */
    private $model;

    public function initialize()
    {
        $this->model = new JieSuanDocModel();
    }

    /**
     * 读取最近消息
     */
    public function index()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];

        $option = $this->option();
        $docId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;

        $where = [];
        $where[] = ['doc_id', "=", $docId];
        $data = $this->model->pageData($option, '', $where);

        $result['status'] = 'ok';
        $result['msg'] = '查找成功';
        $result["data"] = $data;

        return json($result);
    }

    public function sign()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];

        $option = $this->option();
        $docId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;
        $id = $this->request->param("id", 0);
        if (empty($id)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }

        $js = Db::table("gy_jiesuan_doc")
            ->where('doc_id', $docId)
            ->where('id', $id)
            ->find();
        if (empty($js)) {
            $result['msg'] = '订单查找失败';
            return $result;
        }
        if ($js['sign_status'] != 0) {
            $result['msg'] = '订单已签署';
            return $result;
        }

        $updateData = [
            'sign_status' => 1,
            'sign_time' => date("Y-m-d H:i:s")
        ];
        $updateResult = $this->model->editData($id, $updateData);
        if ($updateResult['status'] == 'ok') {
            $result = $updateResult;
            $result['msg'] = "签署成功";
        }
        return json($result);
    }

    public function sendMsg()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];
        $groupId = $this->request->param("gid", 0);
        if (empty($groupId)) {
            $result['msg'] = 'id参数缺少';
            return $result;
        }
        $msg = $this->request->param("msg", "");
        if (empty($msg)) {
            $result['msg'] = '请输入发送内容';
            return $result;
        }
        $msgType = $this->request->param("msg_type", 0);
        if (empty($msgType)) {
            $result['msg'] = 'type参数缺少';
            return $result;
        }
        $msgFrom = $this->request->param("msg_from", 2);
        if (empty($msgFrom)) {
            $result['msg'] = 'msg_from参数缺少';
            return $result;
        }
        $group = Db::table("gy_chat_group")
            ->where("id", $groupId)
            ->find();

        if (empty($group)) {
            $result['msg'] = '非法聊天';
            return $result;
        }

        $insertData = [
            'group_id' => $groupId,
            'msg' => $msg,
            'msg_type' => $msgType,
            'msg_from' => $msgFrom,
            "creat_time" => date("Y-m-d H:i:s")
        ];
        $resultId = Db::table("gy_chat_record")->insertGetId($insertData);
        if ($resultId) {
            //查找最后一个消息
            $lastMsg = Db::table("gy_chat_record")
                ->where('id', $resultId)
                ->find();
            //更新
            $updateData = [
                'msg' => $msg,
                'msg_type' => $msgType,
                'msg_from' => $msgFrom,
                "msg_time" => date("Y-m-d H:i:s")
            ];
            Db::table("gy_chat_group")
                ->where('id', $groupId)
                ->update($updateData);

            $result['status'] = 'ok';
            $result['msg'] = '发送成功';
            $result['data'] = $lastMsg;
        }
        return json($result);
    }

    public function docChat()
    {
        $result = [
            "status" => "no",
            "data" => [],
            "msg" => "查找失败"
        ];

        $option = $this->option();
        $docId = $option['user']['account_id'] ? $option['user']['account_id'] : 0;
        $group = Db::table("gy_chat_group")
            ->where("doc_id", $docId)
            ->order("msg_time", 'desc')
            ->select();

        $result['status'] = 'ok';
        $result['msg'] = '查找成功';
        $result['data'] = $group;

        return json($result);
    }

    //检查是否登录
    private function checkLogin()
    {
        $result = [
            "status" => "noLogin",
            "data" => [],
            "msg" => "请登录"
        ];

        if ($this->user == null) {
            return json($result);
        }
    }
}